package com.the_ring.chapter09.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.the_ring.chapter09.R;
import com.the_ring.chapter09.bean.ItemInfo;

import java.util.List;

public class RecyclerGridAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private List<ItemInfo> infoList;

    public RecyclerGridAdapter(Context context, List<ItemInfo> infoList) {
        this.context = context;
        this.infoList = infoList;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 根据布局文件生成视图对象
        View view = LayoutInflater.from(context).inflate(R.layout.recycler_market_item, parent, false);
        return new ItemHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ItemHolder itemHolder = (ItemHolder) holder;
//        ViewGroup.LayoutParams params = itemHolder.imageView.getLayoutParams();
//        params.height = params.width;
//        itemHolder.imageView.setLayoutParams(params);
//        Log.d("Tag", params.height +  " " + params.width);
        itemHolder.imageView.setImageResource(infoList.get(position).getResId());
        itemHolder.titleTv.setText(infoList.get(position).getTitle());
    }

    @Override
    public int getItemCount() {
        return infoList.size();
    }


    public class ItemHolder extends RecyclerView.ViewHolder {

        private ImageView imageView;
        private TextView titleTv;

        public ItemHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.iv_market_item);
            titleTv = itemView.findViewById(R.id.tv_market_item);
        }
    }
}
